package com.camel.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.core.config.IamApplicationconfig;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.model.SysRole;
import com.camel.core.model.SysUser;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.RsaUtil;
import com.camel.core.utils.ResultUtil;
import com.camel.system.service.SysRoleService;
import com.camel.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.List;

/**
 * <用户控制器>
 *
 * @author baily
 * @date 2019/7/5
 * @since 1.0
 **/
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseCommonController<SysUser> {

    @Autowired
    private SysUserService<SysUser> service;

    @Autowired
    private SysRoleService<SysRole> roleService;

    @Autowired
    private RsaUtil rsaUtil;

    @Autowired
    private IamApplicationconfig iamApplicationconfig;

    @Override
    public boolean exist(SysUser entity) {
        return service.count(new QueryWrapper<SysUser>().eq("login_name", entity.getLoginName())) > 0;
    }

    @Override
    public Result save(@RequestBody SysUser entity) {
        SysRole role = roleService.getOne(new QueryWrapper<SysRole>().eq("role_name", entity.getRoleName()), false);
        if(!ObjectUtils.isEmpty(role)) {
            entity.setRoleId(role.getRoleId());
        }
        Result save = super.save(entity);
        entity.setWorkNum(new DecimalFormat("P000000").format(entity.getUserId()));
        service.updateById(entity);
        return save;
    }

    @PostMapping("/updates")
    private Result updates(@RequestBody SysUser user) {
        for (String userId : user.getUserIds().split(IamApplicationconfig.SYMBLE_COMMA)) {
            SysUser sysUser = new SysUser();
            sysUser.setUserId(Integer.parseInt(userId));
            sysUser.setCompanyId(user.getCompanyId());
            sysUser.setCompany(user.getCompany());
            service.updateById(sysUser);
        }
        return ResultUtil.success();
    }

    @Override
    public Result update(@RequestBody SysUser entity) {
        SysRole role = roleService.getOne(new QueryWrapper<SysRole>().eq("role_name", entity.getRoleName()), false);
        if(!ObjectUtils.isEmpty(role)) {
            entity.setRoleId(role.getRoleId());
        }
        return super.update(entity);
    }

    @PostMapping("/resetPasswd")
    public Result restPw(@RequestBody SysUser user) throws Exception {
        // 使用公钥加密
        user.setPassword(rsaUtil.encryptString(iamApplicationconfig.getDefaultPassword()));
        // 使用私钥解密
        return ResultUtil.success(service.updateById(user));
    }

    @GetMapping("/list")
    public Result list(@RequestParam("ids") String ids) {
        List<SysUser> users = getiService().list(new QueryWrapper<SysUser>().in("id", ids.split(",")));
        return ResultUtil.success(users);
    }

    @Override
    public BaseCommonService<SysUser> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "用户";
    }
}

